home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / FWString / FWPStr.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  16.1 KB  |  486 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPStr.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef FWPSTR_H
  13. #include "FWPStr.h"
  14. #endif
  15.  
  16. #ifndef SLSTRREP_H
  17. #include "SLStrRep.h"
  18. #endif
  19.  
  20. #ifndef FWSTREAM_H
  21. #include "FWStream.h"
  22. #endif
  23.  
  24. #ifndef FWMEMHLP_H
  25. #include "FWMemHlp.h"
  26. #endif
  27.  
  28. #ifndef FWEXCEPT_H
  29. #include "FWExcept.h"
  30. #endif
  31.  
  32. //========================================================================================
  33. // File scope definitions
  34. //========================================================================================
  35.  
  36. #ifdef FW_BUILD_MAC
  37. #pragma segment Strings
  38. #endif
  39.  
  40. //========================================================================================
  41. //    CLASS FW_CString
  42. //========================================================================================
  43.  
  44. FW_DEFINE_AUTO(FW_CString)
  45. FW_DEFINE_CLASS_M0(FW_CString)
  46.  
  47. // This class is archivable, but we provide the archiving implementation in a separate
  48. // translation unit in order to enable deadstripping of the archiving-related code
  49. // in parts that do not use archiving with this class.
  50.  
  51. //----------------------------------------------------------------------------------------
  52. //    FW_CString::~FW_CString
  53. //----------------------------------------------------------------------------------------
  54.  
  55. FW_CString::~FW_CString()
  56. {
  57.     FW_START_DESTRUCTOR
  58.     FW_PrivString_Release(fRep);
  59. }
  60.  
  61. //----------------------------------------------------------------------------------------
  62. //    FW_CString::FW_CString
  63. //----------------------------------------------------------------------------------------
  64.  
  65. FW_CString::FW_CString()
  66.     : fRep(FW_PrivString_AcquireEmptyString())
  67. {
  68.     FW_END_CONSTRUCTOR
  69. }
  70.  
  71. //----------------------------------------------------------------------------------------
  72. //    FW_CString::FW_CString
  73. //----------------------------------------------------------------------------------------
  74.  
  75. FW_CString::FW_CString(ODIText *text)
  76.     : fRep(FW_PrivString_AcquireEmptyString())
  77. {
  78.     ReplaceAll(text);
  79.     FW_END_CONSTRUCTOR
  80. }
  81.  
  82. //----------------------------------------------------------------------------------------
  83. //    FW_CString::FW_CString
  84. //----------------------------------------------------------------------------------------
  85.  
  86. FW_CString::FW_CString(FW_HString rep)
  87.     : fRep(rep)
  88. {
  89.     FW_PrivString_Acquire(fRep);
  90.     FW_END_CONSTRUCTOR
  91. }
  92.  
  93. //----------------------------------------------------------------------------------------
  94. //    FW_CString::FW_CString
  95. //----------------------------------------------------------------------------------------
  96.  
  97. FW_CString::FW_CString(const FW_CString& other)
  98.     : fRep(other.fRep)
  99. {
  100.     FW_PrivString_Acquire(fRep);
  101.     FW_END_CONSTRUCTOR
  102. }
  103.  
  104. //----------------------------------------------------------------------------------------
  105. //    FW_CString::FW_CString
  106. //----------------------------------------------------------------------------------------
  107.  
  108. FW_CString::FW_CString(const char* string)
  109.     : fRep(FW_PrivString_AcquireEmptyString())
  110. {
  111.     FW_PlatformError error = 0;
  112.     FW_HString rep = FW_PrivString_ReplaceAllBytes(fRep, string, FW_PrimitiveStringLength(string), &error);
  113.     FW_FailOnError(error);
  114.     fRep = rep;
  115.     FW_END_CONSTRUCTOR
  116. }
  117.  
  118. //----------------------------------------------------------------------------------------
  119. //    FW_CString::FW_CString
  120. //----------------------------------------------------------------------------------------
  121.  
  122. FW_CString::FW_CString(const char* bytes, FW_ByteCount byteLength)
  123.     : fRep(FW_PrivString_AcquireEmptyString())
  124. {
  125.     FW_PlatformError error = 0;
  126.     FW_HString rep = FW_PrivString_ReplaceAllBytes(fRep, bytes, byteLength, &error);
  127.     FW_FailOnError(error);
  128.     fRep = rep;
  129.     FW_END_CONSTRUCTOR
  130. }
  131.  
  132. //----------------------------------------------------------------------------------------
  133. //    FW_CString::FW_CString
  134. //----------------------------------------------------------------------------------------
  135. FW_CString::FW_CString(const char* string, const FW_Locale& locale)
  136.     : fRep(0)
  137. {
  138.     FW_PlatformError error = 0;
  139.     FW_HString rep = FW_PrivString_AcquireEmptyStringWithLocale(locale, &error);
  140.     FW_FailOnError(error);
  141.     rep = FW_PrivString_ReplaceAllBytes(rep, string, FW_PrimitiveStringLength(string), &error);
  142.     FW_FailOnError(error);
  143.     fRep = rep;
  144.     FW_END_CONSTRUCTOR
  145. }
  146.  
  147. //----------------------------------------------------------------------------------------
  148. //    FW_CString::FW_CString
  149. //----------------------------------------------------------------------------------------
  150.  
  151. FW_CString::FW_CString(const char* bytes, FW_ByteCount byteLength, const FW_Locale& locale)
  152.     : fRep(0)
  153. {
  154.     FW_PlatformError error = 0;
  155.     FW_HString rep = FW_PrivString_AcquireEmptyStringWithLocale(locale, &error);
  156.     FW_FailOnError(error);
  157.     rep = FW_PrivString_ReplaceAllBytes(rep, bytes, byteLength, &error);
  158.     FW_FailOnError(error);
  159.     fRep = rep;
  160.     FW_END_CONSTRUCTOR
  161. }
  162.  
  163. //----------------------------------------------------------------------------------------
  164. //    FW_CString::FW_CString
  165. //----------------------------------------------------------------------------------------
  166.  
  167. FW_CString::operator FW_HString*()
  168. {
  169.     FW_PlatformError error = 0;
  170.     FW_HString rep = FW_PrivString_LockString(fRep, &error);
  171.     FW_FailOnError(error);
  172.     fRep = rep;
  173.     return &fRep;
  174. }
  175.  
  176. //----------------------------------------------------------------------------------------
  177. //    FW_CString::operator[]
  178. //----------------------------------------------------------------------------------------
  179.  
  180. FW_LChar FW_CString::operator[](FW_CharacterPosition position) const
  181. {
  182.     FW_Locale locale;
  183.     GetLocale(locale);
  184.     if (FW_LocaleIsSingleByte(locale))
  185.     {
  186.         char ch;
  187.         FW_PrivString_Retrieve(fRep, &ch, 1, position);
  188.         return ch;
  189.     }
  190.  
  191.     // Get the byte position
  192.     FW_PlatformError error = 0;
  193.     ODIText* tp = RevealODIText();
  194.     FW_BytePosition bytePosition = FW_TextParams_GetBytePosition(tp, position, &error);
  195.     FW_FailOnError(error);
  196.  
  197.     if (!FW_TextParams_IsCharacterStart(tp, 0, bytePosition, &error))
  198.     {
  199.         // second of two-byte char - position must be wrong
  200.         error = FW_xUnknownError;
  201.     }
  202.     else    // either first of two-byte, or single-byte character
  203.     {
  204.         // Check the next byte. If it's also the first, we know that our character is a single
  205.         if (bytePosition+1 >= GetByteLength() || FW_TextParams_IsCharacterStart(tp, 0, bytePosition+1, &error))
  206.         {
  207.             char ch;
  208.             FW_PrivString_Retrieve(fRep, &ch, 1, bytePosition);
  209.             return ch;
  210.         }
  211.     }
  212.     FW_FailOnError(error);
  213.  
  214.     // Retrieve a two-byte character
  215.     FW_LChar theChar;
  216.     FW_PrivString_Retrieve(fRep, (char*)&theChar, 2, bytePosition);
  217.     return theChar;
  218. }
  219.  
  220. //----------------------------------------------------------------------------------------
  221. // FW_CString::operator=
  222. //----------------------------------------------------------------------------------------
  223.  
  224. FW_CString& FW_CString::operator=(const FW_CString& other)
  225. {
  226.     if (&other != this)
  227.     {
  228.         ReplaceAll(other);
  229.     }
  230.     return *this;
  231. }
  232.  
  233. //----------------------------------------------------------------------------------------
  234. // FW_CString::GrowCapacity
  235. //----------------------------------------------------------------------------------------
  236.  
  237. FW_ByteCount FW_CString::GrowCapacity(FW_ByteCount numberBytes)
  238. {
  239.     FW_PlatformError error = 0;
  240.     FW_HString rep = FW_PrivString_SetCapacity(fRep, numberBytes, &error);
  241.     FW_FailOnError(error);
  242.     fRep = rep;
  243.     return FW_PrivString_GetCapacity(fRep);
  244. }
  245.  
  246. //----------------------------------------------------------------------------------------
  247. // FW_CString::PrivRead
  248. //----------------------------------------------------------------------------------------
  249.  
  250. FW_CReadableStream& FW_CString::PrivRead(FW_CReadableStream& stream)
  251. {
  252.     FW_Locale locale;
  253.     
  254.     stream >> locale.fScriptCode >> locale.fLangCode;
  255.  
  256.     short byteCount;
  257.     stream >> byteCount;
  258.     
  259.     FW_CAcquireTemporarySystemHandle handle(byteCount);
  260.     char* buffer = (char*) handle.GetPointer();
  261.  
  262.     stream.Read(buffer, byteCount);
  263.     FW_CString temp(buffer, byteCount, locale);
  264.     
  265.     ReplaceAll(temp);
  266.     
  267.     return stream;
  268. }
  269.  
  270. //----------------------------------------------------------------------------------------
  271. // FW_CString::PrivWrite
  272. //----------------------------------------------------------------------------------------
  273.  
  274. FW_CWritableStream& FW_CString::PrivWrite(FW_CWritableStream& stream) const
  275. {
  276.     FW_Locale locale;
  277.     GetLocale(locale);
  278.  
  279.     stream << locale.fScriptCode << locale.fLangCode;
  280.  
  281.     FW_ByteCount longByteCount = GetByteLength();
  282.     FW_ASSERT(longByteCount < 32768L);
  283.     short byteCount = (short) longByteCount;
  284.     stream << byteCount;
  285.     const char* buffer = RevealBuffer();
  286.     stream.Write(buffer, byteCount);
  287.     return stream;
  288. }
  289.  
  290. //----------------------------------------------------------------------------------------
  291. //    FW_CString::GetCharacterLength
  292. //----------------------------------------------------------------------------------------
  293.  
  294. FW_CharacterCount FW_CString::GetCharacterLength() const
  295. {
  296.     FW_PlatformError error = 0;
  297.     FW_CharacterCount result = FW_PrivString_GetCharacterLength(fRep, &error);
  298.     FW_FailOnError(error);
  299.     return result;
  300. }
  301.  
  302. //----------------------------------------------------------------------------------------
  303. //    FW_CString::ParseAsSignedInteger
  304. //----------------------------------------------------------------------------------------
  305.  
  306. long    FW_CString::ParseAsSignedInteger() const
  307. {
  308.     return FW_PrivString_DecimalStringToSignedInteger(fRep);
  309. }
  310.  
  311. //----------------------------------------------------------------------------------------
  312. //    FW_CString::ParseAsUnsignedInteger
  313. //----------------------------------------------------------------------------------------
  314.  
  315. unsigned long    FW_CString::ParseAsUnsignedInteger() const
  316. {
  317.     return FW_PrivString_DecimalStringToUnsignedInteger(fRep);
  318. }
  319.  
  320. //----------------------------------------------------------------------------------------
  321. //    FW_CString::ParseAsHexadecimalInteger
  322. //----------------------------------------------------------------------------------------
  323.  
  324. unsigned long    FW_CString::ParseAsHexadecimalInteger() const
  325. {
  326.     return FW_PrivString_HexadecimalStringToUnsignedInteger(fRep);
  327. }
  328.  
  329. //----------------------------------------------------------------------------------------
  330. //    FW_CString::ParseAsRealNumber
  331. //----------------------------------------------------------------------------------------
  332.  
  333. double    FW_CString::ParseAsRealNumber() const
  334. {
  335.     return FW_PrivString_StringToDouble(fRep);
  336. }
  337.  
  338. //----------------------------------------------------------------------------------------
  339. //    FW_CString::ReplaceAllAsSignedDecimalInteger
  340. //----------------------------------------------------------------------------------------
  341.  
  342. void    FW_CString::ReplaceAllAsSignedDecimalInteger(long integer)
  343. {
  344.     FW_PlatformError error = 0;
  345.     FW_HString rep = FW_PrivString_SignedIntegerToDecimalString(fRep, integer, &error);
  346.     FW_FailOnError(error);
  347.     fRep = rep;
  348. }
  349.  
  350. //----------------------------------------------------------------------------------------
  351. //    FW_CString::ReplaceAllAsUnsignedDecimalInteger
  352. //----------------------------------------------------------------------------------------
  353.  
  354. void    FW_CString::ReplaceAllAsUnsignedDecimalInteger(unsigned long integer)
  355. {
  356.     FW_PlatformError error = 0;
  357.     FW_HString rep = FW_PrivString_UnsignedIntegerToDecimalString(fRep, integer, &error);
  358.     FW_FailOnError(error);
  359.     fRep = rep;
  360. }
  361.  
  362. //----------------------------------------------------------------------------------------
  363. //    FW_CString::ReplaceAllAsHexadecimalInteger
  364. //----------------------------------------------------------------------------------------
  365.  
  366. void    FW_CString::ReplaceAllAsHexadecimalInteger(unsigned long integer)
  367. {
  368.     FW_PlatformError error = 0;
  369.     FW_HString rep = FW_PrivString_UnsignedIntegerToHexadecimalString(fRep, integer, &error);
  370.     FW_FailOnError(error);
  371.     fRep = rep;
  372. }
  373.  
  374. //----------------------------------------------------------------------------------------
  375. //    FW_CString::ReplaceAllAsRealNumber
  376. //----------------------------------------------------------------------------------------
  377.  
  378. void    FW_CString::ReplaceAllAsRealNumber(double real, short fracDigits)
  379. {
  380.     FW_PlatformError error = 0;
  381.     FW_HString rep = FW_PrivString_DoubleToString(fRep, real, fracDigits, &error);
  382.     FW_FailOnError(error);
  383.     fRep = rep;
  384. }
  385.     
  386. //========================================================================================
  387. //    CLASS FW_CAcquireNulTerminatedString
  388. //========================================================================================
  389.  
  390. FW_DEFINE_AUTO(FW_CAcquireNulTerminatedString)
  391.  
  392. //----------------------------------------------------------------------------------------
  393. //    FW_CAcquireNulTerminatedString::FW_CAcquireNulTerminatedString
  394. //----------------------------------------------------------------------------------------
  395.  
  396. FW_CAcquireNulTerminatedString::FW_CAcquireNulTerminatedString(const FW_CString& string)
  397.     : fExportedString(NULL)
  398. {
  399.     fExportedString = new char[string.GetByteLength()+1];
  400.     string.ExportCString(fExportedString);
  401.     FW_END_CONSTRUCTOR
  402. }
  403.  
  404. //----------------------------------------------------------------------------------------
  405. //    FW_CAcquireNulTerminatedString::~FW_CAcquireNulTerminatedString
  406. //----------------------------------------------------------------------------------------
  407.  
  408. FW_CAcquireNulTerminatedString::~FW_CAcquireNulTerminatedString()
  409. {
  410.     FW_START_DESTRUCTOR
  411.     delete [] fExportedString;
  412. }
  413.  
  414. //========================================================================================
  415. //    CLASS FW_CAcquireNulTerminatedString255
  416. //========================================================================================
  417.  
  418. //----------------------------------------------------------------------------------------
  419. //    FW_CAcquireNulTerminatedString255::FW_CAcquireNulTerminatedString255
  420. //----------------------------------------------------------------------------------------
  421.  
  422. FW_CAcquireNulTerminatedString255::FW_CAcquireNulTerminatedString255(const FW_CString& string)
  423. {
  424.     FW_PRIV_ASSERT(string.GetByteLength() < sizeof(fExportedString));
  425.     string.ExportCString(fExportedString);
  426. }
  427.  
  428. //========================================================================================
  429. //    Global FW_CString compare functions
  430. //========================================================================================
  431.  
  432. //----------------------------------------------------------------------------------------
  433. //    operator==
  434. //----------------------------------------------------------------------------------------
  435.  
  436. FW_Boolean operator==(const FW_CString& string1, const FW_CString& string2)
  437. {
  438.     return FW_PrivString_Compare(string1.fRep, string2.fRep) == FW_kStringsEqual;
  439. }
  440.  
  441. //----------------------------------------------------------------------------------------
  442. //    operator!=
  443. //----------------------------------------------------------------------------------------
  444.  
  445. FW_Boolean operator!=(const FW_CString& string1, const FW_CString& string2)
  446. {
  447.     return FW_PrivString_Compare(string1.fRep, string2.fRep) != FW_kStringsEqual;
  448. }
  449.  
  450. //----------------------------------------------------------------------------------------
  451. //    operator<
  452. //----------------------------------------------------------------------------------------
  453.  
  454. FW_Boolean operator<(const FW_CString& string1, const FW_CString& string2)
  455. {
  456.     return FW_PrivString_Compare(string1.fRep, string2.fRep) == FW_kStringOneLess;
  457. }
  458.  
  459. //----------------------------------------------------------------------------------------
  460. //    operator>
  461. //----------------------------------------------------------------------------------------
  462.  
  463. FW_Boolean operator>(const FW_CString& string1, const FW_CString& string2)
  464. {
  465.     return FW_PrivString_Compare(string1.fRep, string2.fRep) == FW_kStringOneGreater;
  466. }
  467.  
  468. //----------------------------------------------------------------------------------------
  469. //    operator<=
  470. //----------------------------------------------------------------------------------------
  471.  
  472. FW_Boolean operator<=(const FW_CString& string1, const FW_CString& string2)
  473. {
  474.     return FW_PrivString_Compare(string1.fRep, string2.fRep) != FW_kStringOneGreater;
  475. }
  476.  
  477. //----------------------------------------------------------------------------------------
  478. //    operator>=
  479. //----------------------------------------------------------------------------------------
  480.  
  481. FW_Boolean operator>=(const FW_CString& string1, const FW_CString& string2)
  482. {
  483.     return FW_PrivString_Compare(string1.fRep, string2.fRep) != FW_kStringOneLess;
  484. }
  485.  
  486.